﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Text.RegularExpressions;

/// <summary>
/// Summary description for PageHelper
/// </summary>
public class PageHelper
{
    public PageHelper()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    public static string ApplicationPath
    {
        get
        {
            return HttpContext.Current.Request.ApplicationPath;
        }
    }
    public static string GetWebConfigValue(string key)
    {
        return System.Configuration.ConfigurationManager.AppSettings[key];
    }

   
    #region Javascsript常见的弹出窗口(重载1次)
    /// <summary>
    /// javascsript常见的弹出窗口
    /// </summary>
    /// <param name="Msg">要显示的提示信息</param>
    /// <param name="Url">要转到的网站地址</param>
    /// <returns></returns>
    public static void AlterMsgAndRedirect(string Msg, string Url)
    {
        string resultstring = "";
        resultstring = "<script language=\"javascript\">";
        resultstring += "alert(\"" + Msg + "\");";
        resultstring += "window.location=\"" + Url + "\";";
        resultstring += "</script>";
        System.Web.HttpContext.Current.Response.Write(resultstring);
    }
    /// <summary>
    /// 重载AlterMsgAndRedirect
    /// </summary>
    /// <param name="page"></param>
    /// <param name="message"></param>
    /// <param name="gotoUrl"></param>
    public static void AlterMsgAndRedirect(Page page, String message, Uri gotoUrl)
    {
        AlterMsgAndRedirect(page, message, gotoUrl.ToString());
    }

    public static void AlterMsgAndRedirect(Page page, String message, String gotoUrlString)
    {
        String script = "alert('" + message + "');";
        script += "window.location.href='" + gotoUrlString + "';";
        page.ClientScript.RegisterStartupScript(page.GetType(), "", script, true);
    }

    /// <summary>
    /// javascsript常见的弹出窗口
    /// </summary>
    /// <param name="Msg">要显示的提示信息</param>
    /// <returns></returns>
    public static void AlterMsg(string Msg)
    {
        string resultstring = "";
        resultstring = "<script language=\"javascript\">";
        resultstring += "alert(\"" + Msg + "\");";
        resultstring += "window.history.go(-1);";
        resultstring += "</script>";
        System.Web.HttpContext.Current.Response.Write(resultstring);
    }

    public static void AlterMsg(Page page, String message)
    {
        page.ClientScript.RegisterStartupScript(page.GetType(), "", "alert('" + message + "');", true);
    }

    //去除文件中的HTML标签
    public static string NoHTML(string Htmlstring)
    {

        //删除脚本

        Htmlstring = Regex.Replace(Htmlstring, @"<[^>]*?>.*?</>", "", RegexOptions.IgnoreCase);

        //删除HTML

        Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);

        Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);

        Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);

        Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);

        Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);

        Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);

        Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);

        Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);

        Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);

        Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);

        Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);

        Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);

        Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);

        Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"&(rdquo|#8221);", "”", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"&(mdash|#8212);", "—", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"&(ldquo|#8220)", "“", RegexOptions.IgnoreCase);


        Htmlstring.Replace("<", "");

        Htmlstring.Replace(">", "");

        Htmlstring.Replace("\r\n", "");

        Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();


        return Htmlstring;

    }

    #endregion


}

